package com.hpin.fansea.service;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.CommentAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.CommentList;
import com.hpin.fansea.bean.CommentResultBean;
import com.hpin.fansea.bean.SurroundBussniseList;
import com.hpin.fansea.home.SurroundCommentActivity;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.hpin.fansea.widget.PreviewPhotoActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 周边商家详情页面
 * Created by Aaron on 15/9/11.
 */
public class SurroundDetailActivity extends BaseActivity {

    public static final int SURROUND_COMMENT = 1003;

    private ListView surround_detail_commentList;

    private ImageView surround_detail_comment;
    private TextView surround_detail_name;
    private ImageView surround_detail_photo;
    private TextView surround_detail_address;

    private SurroundBussniseList surroundDetail;

    private TextView surround_detail_phone;
    private TextView surround_detail_title;

    private CommentAdapter commentAdapter;
    private List<CommentList> commentLists = null;

    private TextView scan_goods_describe;

    private RelativeLayout surround_detail_rlPhone;// 电话的布局
    private Intent mIntent;

    private ImageView surround_photos;
    private TextView tv_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_surround_detail);

        commentLists = new ArrayList<CommentList>();

        surroundDetail = (SurroundBussniseList) getIntent().getExtras().getSerializable("surroundDetail");

        initFindViewByid();

        getCommentLists();

    }

    /**
     * 获取评论列表
     */
    private void getCommentLists() {

        // 请求评论列表
        if (surroundDetail != null) {

            String footId = surroundDetail.id_;
            initData(surroundDetail);
            getNetData(footId);

        }
    }

    private void getNetData(String BussniseId) {

        HttpUtil.getInstance().setGetRequest("获取中...", this,
                Constant.WEB_ROOT + Constant.BUSSNISECOMMENTLIST
                        + BussniseId + "?token=" + sp.getString(Constant.token, "") + "&pageSize="
                        + 1000 + "&pageIndex=" + 0
                        + "&systemType=" + Constant.SYSTEM_TYPE, new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            CommentResultBean resultBean = JSONObject.parseObject(str, CommentResultBean.class);

                            if (Constant.STATUS_SUCESS.equals(resultBean.status)) {

                                commentLists = resultBean.result;

                                if (commentLists != null && commentAdapter != null) {
                                    commentAdapter.setData(commentLists);
                                    commentAdapter.notifyDataSetChanged();
                                }

                            } else {
                                showToast(resultBean.message);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast("获取评论数据失败");
                        }

                    }
                }, null);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SURROUND_COMMENT) {

            if (commentLists!=null) {

                if (data == null) {
                    return;
                }

                CommentList comment = (CommentList) data.getSerializableExtra("comment");

                commentLists.add(0, comment);
                commentAdapter.setData(commentLists);
                commentAdapter.notifyDataSetChanged();

            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initFindViewByid() {

        surround_detail_comment = (ImageView) findViewById(R.id.iv_left);
        surround_detail_title = (TextView) findViewById(R.id.tv_title);
        tv_right = (TextView) findViewById(R.id.tv_right);

        tv_right.setText(getResources().getString(R.string.comment));
        tv_right.setOnClickListener(this);

        surround_detail_title.setText(getResources().getString(R.string.business_detail));

        // 评论列表
        surround_detail_commentList = (ListView) findViewById(R.id.surround_detail_commentList);
        // listview的头
        View my_head = getLayoutInflater().inflate(R.layout.head_surround_detail, null);

        commentAdapter = new CommentAdapter(this, commentLists);
        surround_detail_commentList.addHeaderView(my_head);
        surround_detail_commentList.setAdapter(commentAdapter);

        surround_detail_photo = (ImageView) my_head.findViewById(R.id.surround_detail_photo);
        // 商家简介
        scan_goods_describe = (TextView) my_head.findViewById(R.id.scan_goods_describe);
        // 商家简介下的图片
        surround_photos = (ImageView) my_head.findViewById(R.id.surround_photos);
        // 商家名称
        surround_detail_name = (TextView) my_head.findViewById(R.id.surround_detail_name);

//        // 查看优惠券详情
//        surround_detail_rlCoupon = (RelativeLayout) my_head.findViewById(R.id.surround_detail_rlCoupon);
        // 优惠券
//        surround_detail_btnCoupon = (TextView) my_head.findViewById(R.id.surround_detail_btnCoupon);

        surround_detail_rlPhone = (RelativeLayout) my_head.findViewById(R.id.surround_detail_rlPhone);
        surround_detail_address = (TextView) my_head.findViewById(R.id.surround_detail_address);
        surround_detail_phone = (TextView) my_head.findViewById(R.id.surround_detail_phone);
//        surround_detail_phoneIcon = (ImageView) my_head.findViewById(R.id.surround_detail_phoneIcon);

        // 拨打电话布局
        surround_detail_rlPhone.setOnClickListener(this);
        // 点击图片查看大图
        surround_photos.setOnClickListener(this);
        // 评论点击事件
        surround_detail_comment.setOnClickListener(this);

    }

    private void initData(SurroundBussniseList detail) {

        // 商家名称
        surround_detail_name.setText(detail.busName);

        // 商家地址
        surround_detail_address.setText(detail.addr);

        // 商家简介
        scan_goods_describe.setText(detail.busInfo);
//
//        if (!TextUtils.isEmpty(detail.commentOfNB)) {
//            tv_right.setText(detail.commentOfNB + "评论");
//        } else {
//            tv_right.setText("评论");
//        }
        // 座机
        if (!TextUtils.isEmpty(detail.telNo)) {
            surround_detail_phone.setText(detail.telNo);
        } else {
            // 为空就将图标和手机号隐藏
            surround_detail_rlPhone.setVisibility(View.GONE);
        }
        // 图片
        if (!TextUtils.isEmpty(detail.fmpic)) {
            ImageLoader.getInstance().displayImage(detail.fmpic, surround_detail_photo);
        } else {
            surround_detail_photo.setVisibility(View.GONE);
        }
        // 商家简介的图片
        if (detail.photos != null && detail.photos.size() > 0) {
            ImageLoader.getInstance().displayImage(detail.photos.get(0), surround_photos);
        } else {
            surround_photos.setVisibility(View.GONE);
        }
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();
                break;
            case R.id.surround_photos://点击查看大图

                // 点击查看大图
                mIntent = new Intent(SurroundDetailActivity.this, PreviewPhotoActivity.class);

                mIntent.putExtra("picLists", (Serializable) surroundDetail.photos);

                startActivity(mIntent);

                break;
            case R.id.tv_right://评论

                if (!Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
//
                    mIntent = new Intent(this, SurroundCommentActivity.class);

                    if (surroundDetail != null) {

                        mIntent.putExtra("surroundId", surroundDetail.id_);

                    }

                    startActivityForResult(mIntent, SURROUND_COMMENT);
                } else {
                    remindLogin("亲,登录后才能评论哦");
                }

                break;

            case R.id.surround_detail_rlPhone://拨打电话

                // 提示对话框
                new AlertDialog.Builder(SurroundDetailActivity.this).setTitle("提示").setMessage("是否要拨打电话").setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {

                                String phoneNum = surround_detail_phone.getText().toString().trim();

                                if (!TextUtils.isEmpty(phoneNum)) {
                                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
                                    startActivity(intent);
                                }

                            }
                        }).setNegativeButton("取消", null).show();

                break;

        }

    }
}
